#!/bin/sh
function Error_Msg()
{
    error_msg=$1
    echo error_message=$error_msg
    curl -H "Content-Type:application/json"  -X POST --data  '{"errorinfo": [{"error_message":"'"$error_msg"'","jid":"'$rp_jid'"}]}'  "$notify_url"
    exit 1
}

echo jenkins var as follow:
echo build_num=$BUILD_NUMBER
echo build_user_id=$BUILD_USER_ID
echo build_user=$BUILD_USER
echo job_name=$JOB_NAME
echo cur_dir is `pwd`
source /etc/profile
re_url=`cat re_url_info`

cdn_ftp_server="http://cdn.hpplay.com.cn/jenkins_result"
cdn_ftp_path="lebotest@120.76.155.128:/lebores/jenkins_result"
cdn_ftp_psw="lebo123" #CDN密码
cdn_ftp_server_test="http://cdn.hpplay.com.cn/jenkins_result_test"
cdn_ftp_path_test="lebotest@120.76.155.128:/lebores/jenkins_result_test"

#app 要放到cibn上
#cibn_ftp_server="http://source.hpplay.cn/release"
#cibn_url="http://cdn1.hpplay.cn"
cibn_url="http://download.mirrorcast.cn"
cibn_ftp_server="$cibn_url/release"
cibn_ftp_path="lebo@47.106.144.141:/lebosource/apk/release"
#cibn_ftp_psw="source!@#321" #CIBN密码
cibn_ftp_psw="Pass@123(*)" #CIBN密码
#cibn_ftp_server_test="http://source.hpplay.cn/test"
cibn_ftp_server_test="$cibn_url/test"
cibn_ftp_path_test="lebo@47.106.144.141:/lebosource/apk/test"

notify_url_release=$re_url
notify_url_test="http://120.79.212.106:9201/JenkinsCallback2"
#notify_url_test="http://192.168.40.246:9201/JenkinsCallback2"

sed -i 's/,/\n/g' user_config
if [ "$JOB_NAME" != "Yun-Upload" ];then
    echo ================Test Project===========
    notify_url=$notify_url_test
    if [  "$version_type" == "1" ]; then #测试工程app
        ftp_server=$cibn_ftp_server_test
        ftp_path=$cibn_ftp_path_test
        ftp_psw=$cibn_ftp_psw
    else #测试工程sdk
        ftp_server=$cdn_ftp_server_test
        ftp_path=$cdn_ftp_path_test
        ftp_psw=$cdn_ftp_psw
    fi
else
    notify_url=$notify_url_release
    if [  "$version_type" == "1" ]; then #正式工程app
        ftp_server=$cibn_ftp_server
        ftp_path=$cibn_ftp_path
        ftp_psw=$cibn_ftp_psw
    else #正式工程sdk
        ftp_server=$cdn_ftp_server
        ftp_path=$cdn_ftp_path
        ftp_psw=$cdn_ftp_psw
    fi
fi

version_ftp=`grep  ftp_pwd user_config |awk -F '=' '{print $2}'`
version_type=`grep version_type user_config |awk -F '=' '{print $2}'`
version_jid=`grep jid user_config |awk -F '=' '{print $2}'`

echo user config as follow:
cat user_config

#将路径中的\替换成/,不清楚为啥要定向到文件，不能直接赋值给变量（会报错）
#echo $version_ftp  | sed 's/\\/\//g' | sed 's/\/ftp//g' > ftp_info
#ftp_pwd=`cat ftp_info`
ftp_pwd=$version_ftp
path_module=`echo $ftp_pwd | awk -F "output/" '{print $2}' |awk -F "/" '{print $1}' `
echo mofify ftp path to $ftp_pwd,path_module=$path_module

if [ "x$ftp_pwd" == "x" ];then
    error_message="ftp path is not right,exit"
    Error_Msg "$error_message"
fi

if [  "$version_type" != "1" -a "$version_type" != "2" ]; then
    error_message="version type $version_type is not 1 or 2, 1--> apk, 2--> sdk,exit "
    Error_Msg "$error_message"
fi

if [  "$version_type" == "1" -a "$path_module" != "phoenix_app" -a "$path_module" != "phoenix_send_app" -a "$path_module" != "phoenix_ios_app"  ]; then
    error_message="version type 1--> apk but your path is not app path ,not fit,so exit "
    Error_Msg "$error_message"
fi

if [  "$version_type" == "2" -a "$path_module" != "phoenix_sdk" -a  "$path_module" != "phoenix_ios" -a "$path_module" != "phoenix_send_sdk"  ]; then
    error_message="version type 2--> sdk, but your path is not sdk path, not fit,so exit "
    Error_Msg "$error_message"
fi

ftp_last_string=`echo ${ftp_pwd:0,-1}`
echo ftp_last_string=$ftp_last_string
if [ "$ftp_last_string" != "/" ];then
    echo the ftp last string is $ftp_last_string not / and curl will be failed,so add string /
    ftp_pwd=$ftp_pwd/
fi
echo ftp_pwd=$ftp_pwd
curl -u aolingli:ALL1234!  $ftp_pwd > ftp_info
[ $? -ne 0 ] && error_message="download $ftp_pwd failed,maybe your path is not right,exit" && Error_Msg "$error_message"
cat ftp_info
#包类型是apk
if [ "$version_type" == "1" ];then
    if [ "$path_module" == "phoenix_app" -o "$path_module" == "phoenix_send_app" ];then
        grep -rn "hpplay-happytest" ftp_info  > ftp_info_tmp
        grep -rn "release_note.txt" ftp_info  >> ftp_info_tmp
        mv ftp_info_tmp  ftp_info
    fi

fi

#包类型是sdk
if [ "$version_type" == "2" ];then
    if [ "$path_module" == "phoenix_ios" ];then
        grep -rn "framework.zip$" ftp_info > ftp_info_tmp
        grep -rn ".txt$" ftp_info >> ftp_info_tmp
        mv ftp_info_tmp ftp_info
    fi

    if [ "$path_module" == "phoenix_send_sdk" ];then
        curl -u aolingli:ALL1234!  "$ftp_pwd"public/ > public_info
        [ $? -ne 0 ] && error_message="download $ftp_pwd public failed,maybe your path is not right,exit" && Error_Msg "$error_message"
    fi

    if [ "$path_module" == "phoenix_sdk" ];then
        grep -rn "update" ftp_info >> ftp_info_tmp
        grep -rn "libs.zip$" ftp_info >> ftp_info_tmp
        mv ftp_info_tmp ftp_info
        curl -u aolingli:ALL1234!  "$ftp_pwd"public/ > public_info
        [ $? -ne 0 ] && error_message="download $ftp_pwd public failed,maybe your path is not right,exit" && Error_Msg "$error_message"
    fi
fi

echo this is phoenix $path_module
download_dir=download_$path_module
mkdir $download_dir
phoenix_path=$ftp_path/$path_module
phoenix_server=$ftp_server/$path_module
echo env config as follow:
echo ftp_server=$ftp_server
echo ftp_path=$ftp_path
echo notify_url=$notify_url
echo phoenix_path=$phoenix_path
echo phoenix_server=$phoenix_server
echo ftp_psw=$ftp_psw
echo ftp path=$version_ftp,version_type=$version_type,jid=$version_jid

if [ "$path_module" != "phoenix_send_sdk" ];then
    echo will download file:
    cat ftp_info
    version_line=`cat ftp_info |wc -l`
    for ((i=1;i<=$version_line;i++))
        do
            download_file=`sed -n "$i"p ftp_info | awk -F " " '{print $4}' `
            echo download_file=$download_file
            curl -u aolingli:ALL1234! -o $download_dir/"$download_file"  "$ftp_pwd"$download_file
        done
fi

if [ "$path_module" == "phoenix_sdk" -o "$path_module" == "phoenix_send_sdk" ];then
    echo $version_ftp  public file as follow:
    cat public_info
    public_line=`cat public_info |wc -l`
    for ((j=1;j<=$public_line;j++))
        do
            public_dfile=`sed -n "$j"p public_info | awk -F " " '{print $4}' `
            echo download_file=$public_dfile
            curl -u aolingli:ALL1234! -o $download_dir/"$public_dfile"  "$ftp_pwd"public/$public_dfile 
        done
fi

cd $download_dir
echo download files as follow:
ls
if [ ! -f release_note.txt ];then #判断下载的文件是否有release_note.txt文件
    error_message="have no release_note.txt file,please check your path file,exit" && Error_Msg "$error_message"
fi
ver_now_time=`date "+%Y%m%d%H%M"`

if [ "$version_type" == "1" ];then
    mv release_note.txt "$ver_now_time"_"$path_module"_release_note.txt
    sshpass -p $ftp_psw  scp  "$ver_now_time"_"$path_module"_release_note.txt $phoenix_path/
    [ $? -ne 0 ] && error_message="upload to cdn failed,exit" && Error_Msg "$error_message"
    phoenix_app_release_note="$phoenix_server/"$ver_now_time"_"$path_module"_release_note.txt"
    if [ "$path_module" == "phoenix_ios_app" ];then
        apk_ver=`ls |grep ".ipa$" |grep "release" |awk -F ".ipa" '{print $1}' |awk -F "Payload_" '{print $2}' | sed 's/\.//g' `
        echo apk version="$apk_ver"
        md5sum *.ipa > md5sum.txt
        app_name_path="$ver_now_time"_"$path_module"_"$apk_ver"
        zip -r "$app_name_path".zip  *.ipa md5sum.txt
        sshpass -p $ftp_psw  scp  "$app_name_path".zip $phoenix_path/
        [ $? -ne 0 ] && error_message="upload to cdn failed,exit" && Error_Msg "$error_message"
        phoenix_download_apk="$phoenix_server/"$app_name_path".zip"
        apk_md5sum=`md5sum "$app_name_path".zip |awk -F " " '{print $1}'`
    else
        apk_ver=`ls |grep ".apk$" |awk -F "-v" '{print $2}' |awk -F ".apk" '{print $1}' |awk -F "_" '{print $1}' | sed 's/\.//g' `
        echo apk version="$apk_ver"
        apk_name=`ls |grep ".apk" `
        md5sum *.apk > md5sum.txt
        app_name_path="$ver_now_time"_"$apk_name"
        mv $apk_name $app_name_path
        aapt_command="/usr/local/scripts/build_scripts/sign_apk_tool/aapt"
        #获取apk的VersionCode,返给乐播云
        apk_vcode=`$aapt_command dump badging $app_name_path |grep "versionCode" |awk -F "versionCode='" '{print $2}'|awk -F "' " '{print $1}'`
        echo apk versionCode=$apk_vcode
        sshpass -p $ftp_psw  scp  "$app_name_path" $phoenix_path/
        [ $? -ne 0 ] && error_message="upload to cdn failed,exit" && Error_Msg "$error_message"
        phoenix_download_apk="$phoenix_server/$app_name_path"
        apk_md5sum=`md5sum *.apk |awk -F " " '{print $1}'`
    fi

    echo all_pkg_download_path=$phoenix_download_apk
    echo release_note_path=$phoenix_app_release_note

    curl -H "Content-Type:application/json"  -X POST --data '{"dlinfo2": [{"result_path": "'$phoenix_download_apk'","jid": "'$version_jid'","aversion": "'$apk_vcode'","bversion": "null","furl": "'$phoenix_download_apk'","fmd5": "'$apk_md5sum'","purl": "null","pmd5": "null" }]}' $notify_url 
fi

cdn_pname=`echo "$ftp_pwd" |awk -F "/" '{print $(NF-1)}'` #以斜杠做分割符，获取ftp路径的倒数第一列
if [ "$version_type" == "2" -a "$path_module" == "phoenix_sdk" ];then
    echo ============================android phoenix receiver sdk==========================

    update_name=`ls |grep "^update" `
    new_update_name="$cdn_pname"_"update"_"$ver_now_time".zip
    mv $update_name $new_update_name
    update_md5sum=`md5sum "$new_update_name" | awk -F " " '{print $1}' `
    echo will upload file $new_update_name to ftp server dir $phoenix_path
    sshpass -p $ftp_psw  scp  "$new_update_name" $phoenix_path/
    [ $? -ne 0 ] && error_message="upload to cdn failed,exit" && Error_Msg "$error_message"
    update_url="$phoenix_server/$new_update_name"
    sdk_version_tmp=`echo "$update_name" |grep update |awk -F "." '{print $2}'`
    sdk_v1=`echo $sdk_version_tmp |awk -F "-" '{print $1}'`
    sdk_v2=`echo $sdk_version_tmp | awk -F "-" '{print $2}'`
    rm  -rf "$new_update_name"

    new_so_name="$cdn_pname"_"so"_"$ver_now_time".zip
    mv libs.zip "$new_so_name"
    so_md5sum=`md5sum "$new_so_name" |awk -F " " '{print $1}'`
    echo will upload file "$new_so_name" to ftp server dir $phoenix_path
    sshpass -p $ftp_psw  scp  "$new_so_name" $phoenix_path/
    [ $? -ne 0 ] && error_message="upload to cdn failed,exit" && Error_Msg "$error_message"
    so_url="$phoenix_server/$new_so_name"
    rm -rf "$new_so_name"

    md5sum * > md5sum.txt
    zip_all_name="$cdn_pname"_"$path_module"_"$ver_now_time".zip
    zip -r $zip_all_name  *
    echo will upload file $zip_all_name to ftp server dir $phoenix_path
    sshpass -p $ftp_psw  scp  $zip_all_name $phoenix_path/
    [ $? -ne 0 ] && error_message="upload to cdn failed,exit" && Error_Msg "$error_message"
    phoenix_download_sdk="$phoenix_server/$zip_all_name"

    echo whole_version=$sdk_version_tmp
    echo all_pkg_download_path=$phoenix_download_sdk
    echo update_pkg_download_path=$update_url
    echo so_pkg_download_path=$so_url

    curl -H "Content-Type:application/json"  -X POST --data '{"dlinfo2": [{"result_path": "'$phoenix_download_sdk'","jid": "'$version_jid'","aversion": "'$sdk_v1'","bversion": "'$sdk_v2'","furl": "'$update_url'","fmd5": "'$update_md5sum'","purl": "'$so_url'","pmd5": "'$so_md5sum'" }]}' $notify_url 
fi

if [ "$version_type" == "2" ]; then
    if [ "$path_module" == "phoenix_ios" -o "$path_module" == "phoenix_send_sdk" ];then
        md5sum *  > md5sum.txt
        sdk_zip_name="$cdn_pname"_"$path_module"_"$ver_now_time".zip
        zip -r "$sdk_zip_name" *
        osdk_md5sum=`md5sum "$sdk_zip_name" |awk -F " " '{print $1}'`
        echo will upload file "$sdk_zip_name" to ftp server dir $phoenix_path
        sshpass -p $ftp_psw  scp  "$sdk_zip_name" $phoenix_path
        [ $? -ne 0 ] && error_message="upload to cdn failed,exit" && Error_Msg "$error_message"
        phoenix_download_osdk="$phoenix_server/$sdk_zip_name"
        echo all_pkg_download_path=$phoenix_download_osdk

        curl -H "Content-Type:application/json"  -X POST --data '{"dlinfo2": [{"result_path": "'$phoenix_download_osdk'","jid": "'$version_jid'","aversion": "null","bversion": "null","furl": "'$phoenix_download_osdk'","fmd5": "'$osdk_md5sum'","purl": "null","pmd5": "null" }]}' $notify_url 
    fi
fi


#curl -u aolingli:ALL1234! -T $recsdk_file_name.zip  $FTP_SERVER/$recsdk_path/
#echo "sshpass -p $ftp_psw  scp -v $phoenix_path.zip $FTP_DIR/$phoenix_path/ "
#scp 后加 -v 调试
#sshpass -p $ftp_psw  rsync -av --progress  $recsdk_file_name.zip $FTP_DIR/$recsdk_path/
